﻿using DevComponents.DotNetBar.SuperGrid;
using Kimd.Common;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI.Controls
{
    public partial class MotionLimitControl : UserControl
    {
        private string _axisName;
        public string ModuleName { get; set; }
        public MotionLimitControl()
        {
            InitializeComponent();
        }
        public void SetAxis(string axisName)
        {
            this._axisName = axisName;
        }

        private void MotionLimitControl_Load(object sender, EventArgs e)
        {
            InitGrid();
        }
        public void InitGrid()
        {
            List<string> limitStr = new List<string>();
            foreach (LimitType lt in Enum.GetValues(typeof(LimitType)))
            {
                limitStr.Add(EnumHelper.GetEnumDescriptionFromValue(lt));
            }
            this.sGC_LimitAxis.PrimaryGrid.Columns["限制类型"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitAxis.PrimaryGrid.Columns["限制类型"].EditorParams = new object[] { limitStr.ToArray() };

            this.sGC_LimitSensor.PrimaryGrid.Columns["限制类型"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitSensor.PrimaryGrid.Columns["限制类型"].EditorParams = new object[] { limitStr.ToArray() };
            
            this.sGC_LimitAxisSensor.PrimaryGrid.Columns["限制类型"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitAxisSensor.PrimaryGrid.Columns["限制类型"].EditorParams = new object[] { limitStr.ToArray() };

            string[] axisArray = SingletonProvider<MachineResource>.Instance.MachineAxis.Keys.ToArray();
            this.sGC_LimitAxis.PrimaryGrid.Columns["限制轴"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitAxis.PrimaryGrid.Columns["限制轴"].EditorParams = new object[] { axisArray };

            this.sGC_LimitPosition.PrimaryGrid.Columns["限制轴"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitPosition.PrimaryGrid.Columns["限制轴"].EditorParams = new object[] { axisArray };

            this.sGC_LimitPosition.PrimaryGrid.Columns["关联轴"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitPosition.PrimaryGrid.Columns["关联轴"].EditorParams = new object[] { axisArray };

            this.sGC_LimitAxisSensor.PrimaryGrid.Columns["关联轴"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitAxisSensor.PrimaryGrid.Columns["关联轴"].EditorParams = new object[] { axisArray };

            List<string> sensorNames = new List<string>();
            foreach (var item in MachineResource.Instance.MachineSensor)
            {
                if (item.Value.Module.Equals(ModuleName))
                {
                    sensorNames.Add(item.Key);
                }
            }
            this.sGC_LimitSensor.PrimaryGrid.Columns["限制感应器"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitSensor.PrimaryGrid.Columns["限制感应器"].EditorParams = new object[] { sensorNames.ToArray() };

            List<string> allSensorNames = new List<string>();
            foreach (var item in MachineResource.Instance.MachineSensor)
            {
                //if (item.Value.Module.Equals(ModuleName))
                {
                    allSensorNames.Add(item.Key);
                }
            }
            this.sGC_LimitAxisSensor.PrimaryGrid.Columns["限制感应器"].EditorType = typeof(FragrantComboBox);
            this.sGC_LimitAxisSensor.PrimaryGrid.Columns["限制感应器"].EditorParams = new object[] { allSensorNames.ToArray() };
        }
        public void AddLimitPosition(PositionLimit positionLimit)
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = positionLimit;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = positionLimit.SectionStartPosition;

            GridCell gridCell2 = new GridCell();
            GridCell gridCell3 = new GridCell();
            GridCell gridCell4 = new GridCell();
            GridCell gridCell5 = new GridCell();
            gridCell2.Value = positionLimit.SectionEndPosition;
            gridCell3.Value = positionLimit.LimitAxisName;
            gridCell4.Value = positionLimit.LimitStartPosition;
            gridCell5.Value = positionLimit.LimitEndPosition;

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell2);
            gridRow.Cells.Add(gridCell3);
            gridRow.Cells.Add(gridCell4);
            gridRow.Cells.Add(gridCell5);

            GridCell gridCell7 = new GridCell();
            gridCell7.Value = positionLimit.RelatedAxisName;
            gridRow.Cells.Add(gridCell7);
            GridCell gridCell8 = new GridCell();
            gridCell8.Value = positionLimit.RelatedStartPosition;
            gridRow.Cells.Add(gridCell8);
            GridCell gridCell9 = new GridCell();
            gridCell9.Value = positionLimit.RelatedEndPosition;
            gridRow.Cells.Add(gridCell9);
            GridCell gridCell10 = new GridCell();
            gridCell10.Value = positionLimit.IsEnable;
            gridRow.Cells.Add(gridCell10);


            this.sGC_LimitPosition.PrimaryGrid.Rows.Add(gridRow);
        }
        public void AddLimitSensor(SensorLimit sensorLimit)
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = sensorLimit;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = sensorLimit.SectionStartPosition;

            GridCell gridCell2 = new GridCell();
            GridCell gridCell3 = new GridCell();
            GridCell gridCell4 = new GridCell();
            GridCell gridCell5 = new GridCell();
            gridCell2.Value = sensorLimit.SectionEndPosition;
            gridCell3.Value = sensorLimit.LimitSensorName;
            gridCell4.Value = EnumHelper.GetEnumDescriptionFromValue(sensorLimit.SensorLimitType);
            gridCell5.Value = sensorLimit.IsSensed;

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell2);
            gridRow.Cells.Add(gridCell3);
            gridRow.Cells.Add(gridCell4);
            gridRow.Cells.Add(gridCell5);

            GridCell gridCell77 = new GridCell();
            gridCell77.Value = sensorLimit.RelatedAxisName;
            gridRow.Cells.Add(gridCell77);
            GridCell gridCell88 = new GridCell();
            gridCell88.Value = sensorLimit.RelatedStartPosition;
            gridRow.Cells.Add(gridCell88);
            GridCell gridCell99 = new GridCell();
            gridCell99.Value = sensorLimit.RelatedEndPosition;
            gridRow.Cells.Add(gridCell99);

            GridCell gridCell8 = new GridCell();
            gridCell8.Value = sensorLimit.IsEnable;
            gridRow.Cells.Add(gridCell8);

            this.sGC_LimitAxisSensor.PrimaryGrid.Rows.Add(gridRow);
        }
        public void AddLimitAxis(MotionLimit motionLimit)
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = motionLimit;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = EnumHelper.GetEnumDescriptionFromValue(motionLimit.LimitType);

            GridCell gridCell2 = new GridCell();
            GridCell gridCell3 = new GridCell();
            GridCell gridCell4 = new GridCell();
            GridCell gridCell5 = new GridCell();
            gridCell2.Value = motionLimit.LimitAxisName;
            gridCell3.Value = motionLimit.Position;
            gridCell4.Value = motionLimit.HighPosition;
            gridCell5.Value = motionLimit.LowPosition;

            gridRow.Cells.Add(gridCell2);
            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell3);
            gridRow.Cells.Add(gridCell4);
            gridRow.Cells.Add(gridCell5);

            GridCell gridCell8 = new GridCell();
            gridCell8.Value = motionLimit.IsEnable;
            gridRow.Cells.Add(gridCell8);

            this.sGC_LimitAxis.PrimaryGrid.Rows.Add(gridRow);
        }
        public void AddLimitSensor(MotionLimit motionLimit)
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = motionLimit;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = EnumHelper.GetEnumDescriptionFromValue(motionLimit.LimitType);

            GridCell gridCell6 = new GridCell();
            GridCell gridCell7 = new GridCell();
            gridCell6.Value = motionLimit.LimitSensorName;
            gridCell7.Value = motionLimit.IsSensed;

            gridRow.Cells.Add(gridCell6);
            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell7);

            GridCell gridCell8 = new GridCell();
            gridCell8.Value = motionLimit.IsEnable;
            gridRow.Cells.Add(gridCell8);

            this.sGC_LimitSensor.PrimaryGrid.Rows.Add(gridRow);
        }
        public void LoadLimit()
        {
            foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].AxisLimits)
            {
                if (item.LimitType == LimitType.SensorNotSensed ||
                    item.LimitType == LimitType.SensorSensed)
                    AddLimitSensor(item);
                else
                    AddLimitAxis(item);
            }
            foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].PositionLimits)
            {
                AddLimitPosition(item);
            }
            foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].SensorLimits)
            {
                AddLimitSensor(item);
            }
        }
        private void tSMI_Add_Click(object sender, EventArgs e)
        {
            MotionLimit motionLimit = new MotionLimit(LimitType.AxisInPosition, "", 0, 0, 0);
            motionLimit.SetAxis(MachineResource.Instance.MachineAxis[_axisName]);
            this.AddLimitAxis(motionLimit);
            SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].AxisLimits.Add(motionLimit);
        }

        private void tSMI_Remove_Click(object sender, EventArgs e)
        {
            if (sGC_LimitAxis.GetSelectedRows().Count == 0)
                return;
            MotionLimit ml = sGC_LimitAxis.GetSelectedRows()[0].Tag as MotionLimit;
            lock (SingletonProvider<MachineResource>.Instance.GetLockObject())
            {
                SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].AxisLimits.Remove(ml);
            }
            sGC_LimitAxis.PrimaryGrid.Rows.Remove(sGC_LimitAxis.GetSelectedRows()[0]);
        }

        private void tSMI_AddSensor_Click(object sender, EventArgs e)
        {
            MotionLimit motionLimit = new MotionLimit(LimitType.SensorSensed, "", false);
            this.AddLimitSensor(motionLimit);
            SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].AxisLimits.Add(motionLimit);
        }

        private void sGC_LimitAxis_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            //修改
            if (e.GridCell.ColumnIndex == 1)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).LimitType = EnumHelper.GetEnumValueFromDescription<LimitType>(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 0)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).LimitAxisName = e.GridCell.Value.ToString();
            }
            else if (e.GridCell.ColumnIndex == 2)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).Position = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 3)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).HighPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 4)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).LowPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 5)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).IsEnable = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
        }
        public bool Verify()
        {
            int index = 0;
            foreach (GridRow item in sGC_LimitAxis.PrimaryGrid.Rows)
            {
                if(string.IsNullOrEmpty(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"轴限制配置中存在错误:{"限制类型不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[1].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"轴限制配置中存在错误:{"限制对象不能为空!"},第{index + 1}行", true);
                    return false;
                }
                index++;
            }
            index = 0;
            foreach (GridRow item in sGC_LimitSensor.PrimaryGrid.Rows)
            {
                if (string.IsNullOrEmpty(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"感应器限制配置中存在错误:{"限制类型不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[1].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"感应器限制配置中存在错误:{"限制对象不能为空!"},第{index + 1}行", true);
                    return false;
                }
                index++;
            }

            return true;
        }

        private void sGC_LimitSensor_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            //修改
            if (e.GridCell.ColumnIndex == 1)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).LimitType = EnumHelper.GetEnumValueFromDescription<LimitType>(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 0)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).LimitSensorName = e.GridCell.Value.ToString();
            }
            else if (e.GridCell.ColumnIndex == 2)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).IsSensed = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 3)
            {
                (e.GridCell.GridRow.Tag as MotionLimit).IsEnable = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
        }

        private void tSMI_AddPosition_Click(object sender, EventArgs e)
        {
            PositionLimit positionLimit = new PositionLimit();
            positionLimit.IsEnable = true;
            positionLimit.SetAxis(MachineResource.Instance.MachineAxis[_axisName]);
            this.AddLimitPosition(positionLimit);
            SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].PositionLimits.Add(positionLimit);
        }

        private void sGC_LimitPosition_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            //修改
            if (e.GridCell.ColumnIndex == 0)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).SectionStartPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 1)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).SectionEndPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 2)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).LimitAxisName = e.GridCell.Value.ToString();
            }
            else if (e.GridCell.ColumnIndex == 3)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).LimitStartPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 4)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).LimitEndPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 5)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).RelatedAxisName = e.GridCell.Value.ToString();
            }
            else if (e.GridCell.ColumnIndex == 6)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).RelatedStartPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 7)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).RelatedEndPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 8)
            {
                (e.GridCell.GridRow.Tag as PositionLimit).IsEnable = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
        }

        private void tSMI_EditSensor_Click(object sender, EventArgs e)
        {
            if (sGC_LimitSensor.GetSelectedRows().Count == 0)
                return;
            if (MessageBox.Show("确定删除限制吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                foreach (var item in sGC_LimitSensor.GetSelectedRows())
                {
                    MotionLimit ml = (item as GridRow).Tag as MotionLimit;
                    lock (SingletonProvider<MachineResource>.Instance.GetLockObject())
                    {
                        SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].AxisLimits.Remove(ml);
                    }
                    sGC_LimitSensor.PrimaryGrid.Rows.Remove(item);
                }
            }
        }

        private void tSMI_RemovePosition_Click(object sender, EventArgs e)
        {
            if (sGC_LimitPosition.GetSelectedRows().Count == 0)
                return;
            if (MessageBox.Show("确定删除限制吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                foreach (var item in sGC_LimitPosition.GetSelectedRows())
                {
                    lock (SingletonProvider<MachineResource>.Instance.GetLockObject())
                    {
                        PositionLimit pl = (item as GridRow).Tag as PositionLimit;
                        SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].PositionLimits.Remove(pl);
                    }
                    sGC_LimitPosition.PrimaryGrid.Rows.Remove(item);
                }
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SensorLimit sensorLimit = new SensorLimit();
            sensorLimit.IsEnable = true;
            sensorLimit.SetAxis(MachineResource.Instance.MachineAxis[_axisName]);
            this.AddLimitSensor(sensorLimit);
            SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].SensorLimits.Add(sensorLimit);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (sGC_LimitAxisSensor.GetSelectedRows().Count == 0)
                return;
            if (MessageBox.Show("确定删除限制吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                foreach (var item in sGC_LimitAxisSensor.GetSelectedRows())
                {
                    lock (SingletonProvider<MachineResource>.Instance.GetLockObject())
                    {
                        SensorLimit pl = (item as GridRow).Tag as SensorLimit;
                        SingletonProvider<MachineResource>.Instance.MachineAxis[_axisName].SensorLimits.Remove(pl);
                    }
                    sGC_LimitAxisSensor.PrimaryGrid.Rows.Remove(item);
                }
            }
        }

        private void sGC_LimitAxisSensor_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            //修改
            if (e.GridCell.ColumnIndex == 0)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).SectionStartPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 1)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).SectionEndPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 2)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).LimitSensorName = e.GridCell.Value.ToString();
            }
            else if (e.GridCell.ColumnIndex == 3)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).SensorLimitType = EnumHelper.GetEnumValueFromDescription<LimitType>(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 4)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).IsSensed = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 5)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).RelatedAxisName = e.GridCell.Value.ToString();
            }
            else if (e.GridCell.ColumnIndex == 6)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).RelatedStartPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 7)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).RelatedEndPosition = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.ColumnIndex == 8)
            {
                (e.GridCell.GridRow.Tag as SensorLimit).IsEnable = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
        }
    }
}
